#include "MySQLClient.hpp"
#include "Configuration.hpp"
#include <string.h>
#include <stdlib.h>
#include <crypt.h>
#include <unistd.h>
#include <shadow.h>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
using std::atoi;

int main(){
    using namespace wd;
    MySQLClient mysqlclient;
    Configuration* config = Configuration::getInstance("./server.conf");
    map<string,string>& config_map = config->getConfigMap();
    bool ret = mysqlclient.connect(config_map["host"],
                                   atoi(config_map["port"].data()),
                                   config_map["user"],
                                   config_map["passwd"],
                                   config_map["db"]);
#if 0
    bool ret = mysqlclient.connect("192.168.189.128",
                                   3306,
                                   "root",
                                   "123",
                                   "user_data");
#endif
    //读出盐值
    string sql2 = "select setting from login_info where name='april'";
    vector<vector<string>> ret2 = mysqlclient.readQuery(sql2);
    //mysqlclient.show_res(ret2);
    string setting = ret2[1][0];
    cout << "setting:" << setting <<endl;
    
    //获得用户的密码
    char * passwd = getpass("please input your passwd: ");
    printf("get your passwd: ___%s___\n",passwd);
    char * encoded = crypt(passwd,setting.c_str());
    printf("get your encoded: %s\n",encoded);

    //读出密文
    string sql3 = "select encrypt from login_info where name='april'";
    vector<vector<string>> ret3 = mysqlclient.readQuery(sql3);
    string sql_encoded = ret3[1][0];
    cout << "sql_encoded: " <<sql_encoded <<endl;
    
    if(strcmp(encoded,sql_encoded.c_str()) == 0){
        printf("log success\n");
    }


}

